<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row class="btn">
        <el-col>
          <el-button type="primary" @click="showAddCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :expand-type="false"
        :selection-type="false"
        border
        :show-index="true"
      >
        <!-- 是否有效 -->
        <template slot="likes" scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted == false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="sort" scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt">
          <el-button icon="el-icon-edit" type="primary" size="mini"
            >编辑</el-button
          >
          <el-button icon="el-icon-delete" type="danger" size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="cateInfo.pagenum"
        :page-sizes="[3, 5, 8, 10]"
        :page-size="cateInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 添加分类框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="isCateDialog"
        width="50%"
        @close="reAddCate"
      >
        <!-- 内容 -->
        <div>
          <el-form
            :model="addCate"
            :rules="addCateRules"
            ref="addCateRef"
            label-width="80px"
          >
            <el-form-item label="分类名称" prop="cat_name">
              <el-input v-model="addCate.cat_name"></el-input>
            </el-form-item>
            <el-form-item label="父级分类">
              <el-cascader
                v-model="selectId"
                :options="parentsData"
                :props="parentsProps"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer">
          <el-button @click="isCateDialog = false">取 消</el-button>
          <el-button type="primary" @click="addCategory">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      cateInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "likes",
        },
        {
          label: "排序",
          prop: "cat_deleted",
          type: "template",
          template: "sort",
        },
        {
          label: "操作",
          prop: "cat_deleted",
          type: "template",
          template: "opt",
          minWidth: "130px",
        },
      ],
      total: null,
      isCateDialog: false,
      addCate: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      addCateRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
      selectId: null,
      parentsData: [],
      parentsProps: {
        label: "cat_name",
        value: "cat_id",
        children: "children",
        expandTrigger: "hover",
      },
    };
  },
  methods: {
    //获取分类数据
    async getCateList() {
      let { data: res } = await this.$http.get("categories", {
        params: this.cateInfo,
      });
      if (res.meta.status != 200) return this.$message.error("获取数据失败");
      this.cateList = res.data.result;
      console.log(res);
      this.total = res.data.total;
    },
    //当前页显示多少条
    handleSizeChange(val) {
      this.cateInfo.pagesize = val;
      this.getCateList();
    },
    //当前页
    handleCurrentChange(val) {
      this.cateInfo.pagenum = val;
      this.getCateList();
    },
    //添加分类
    showAddCate() {
      this.getParentData();
      this.isCateDialog = true;
    },
    //重置表单
    reAddCate() {
      this.$refs.addCateRef.resetFields();
      this.addCate.cat_pid = ""
      this.addCate.cat_level = ""
      this.selectId = ""
    },
    //获取1，2级分类名称
    async getParentData() {
      let { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });
      console.log(res);
      if (res.meta.status !== 200) return this.$message, error("获取数据失败");
      this.parentsData = res.data;
    },
    //监听分类节点的变化
    handleChange() {
      console.log(this.selectId);
      if (this.selectId.length > 0) {
        this.addCate.cat_pid = this.selectId[this.selectId.length - 1];
        this.addCate.cat_level = this.selectId.length;
        return;
      } else {
        this.addCate.cat_pid = 0;
        this.addCate.cat_level = 0;
      }
    },
    addCategory() {
      this.$refs.addCateRef.validate(async (vaild) => {
        if (!vaild) return;
        let { data: res } = await this.$http.post("categories", this.addCate);
        if (res.meta.status != 201) return this.$message.error("添加分类失败");
        this.$message.success("添加分类成功");
        this.getCateList();
        this.isCateDialog = false;
      });
    },
  },
  created() {
    this.getCateList();
  },
};
</script>

<style lang="less" scoped>
.btn {
  text-align: left;
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>